package top.jiangqiang.crawler.core.http.entities;

import org.dromara.hutool.core.util.ObjUtil;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class QueryParameters implements Serializable {
    private final Map<String, String> paramMap = new HashMap<>();

    public void addParam(String paramName, String value) {
        this.paramMap.put(paramName, value);
    }

    public String getParam(String paramName) {
        return this.paramMap.get(paramName);
    }

    public Map<String, String> getParams() {
        return ObjUtil.cloneByStream(paramMap);
    }

    /**
     * @param queryParameters 优先级更低
     */
    public void merge(QueryParameters queryParameters) {
        if (queryParameters == null) {
            return;
        }
        queryParameters.getParams().forEach(this::mergeParam);
    }

    public void mergeParam(String paramName, String value) {
        if (!this.paramMap.containsKey(paramName)) {
            addParam(paramName, value);
        }
    }

    public void forEach(BiConsumer<? super String, ? super String> action) {
        paramMap.forEach(action);
    }
}
